const {
    getUserRoleByIdServe,
    saveUserRoleServe,
    updateUserRoleServe,
    getRoleListServe
} = require('../serve/userRole.serve')
const {getRoleServe, getRoleById} = require('../serve/role.serve')

const {getUserById, updateUserRoleName} = require('../serve/user.serve')

class UserRoleController {
    async getUserRoleDetailById(ctx) {
        const {id} = ctx.params;
        // const {id: createdByUserId} = ctx.state.user;
        if (!id) {
            return ctx.body = {
                msg: '未上传角色Id',
                status: 1
            }
        }
        try {
            //先验证当前ID是否有对应的用户
            const currentUser = await getUserById(id, true)
            //如果没有返回结果
            if (!currentUser) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到当前用户'
                }
            }
            const currentRoles = await currentUser.getRoles()
            const assignRoleIds = currentRoles ? currentRoles.map(role => {
                return role.id
            }) : []
            const allRolesList = await getRoleListServe(id)
            ctx.body = {
                status: 0,
                msg: '成功',
                data: {
                    allRolesList,
                    assignRoleIds
                },
            }
        } catch (e) {
            ctx.body = {
                status: 1,
                msg: e.message
            }
        }
    }

    async saveUserRole(ctx) {
        const {userId, roleId} = ctx.request.body;
        try {
            //先验证当前ID是否有对应的用户
            let currentUser = await getUserById(userId, true)
            //如果没有返回结果
            if (!currentUser) {
                return ctx.body = {
                    status: 1,
                    msg: '未找到当前用户'
                }
            }
            const currentRole = await getRoleById(roleId)
            let updateUserRoleRes;
            //如果能找到前端传入对应roleId得角色
            if (currentRole) {
                // 再判断当前用户是否已经分配过角色
                const userRole = await currentUser.getRoles();
                //如果没有分配过角色，就是新增，反之是更新
                if (userRole.length === 0) {
                    console.log('新增角色')
                    updateUserRoleRes = await currentUser.addRoles(currentRole)
                } else {
                    console.log('更新角色')
                    updateUserRoleRes = await currentUser.setRoles(currentRole)
                }
                if (!updateUserRoleRes) {
                    throw({message: '分配角色失败'})
                }
            }
            const res = await getUserById(userId, true)
            const currentUserRoleName = res.Roles.map(role => {
                return role.jobTitle
            })
            await updateUserRoleName(userId, currentUserRoleName.join(','))
            ctx.body = {
                status: 0,
                msg: '添加角色成功'
            }
        } catch (e) {
            ctx.body = {
                status: 1,
                msg: e.message
            }
        }
    }
}

module.exports = new UserRoleController();
